package org.example.dao;

import lombok.extern.slf4j.Slf4j;
import org.example.vo.ActivityVO;
import org.example.vo.tempActivityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/12/13
 */

@Slf4j
@Component
public class activityDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void createActivity(tempActivityVO activity) {
        log.debug("createActivity: {}", activity);
        String sql = "insert into activities(name, `description`) VALUES (?, ?);";
        jdbcTemplate.update(sql,
                activity.getName(),
                activity.getDesc()
        );
    }

    public ActivityVO[] getAllActivity() {
        log.debug("queryAll");
        String sql = "select id, name, `description` from activities";
        return jdbcTemplate.query(sql, this::activityTransform).toArray(new ActivityVO[0]);
    }

    public boolean deleteActivity(int id) {
        log.debug("deleteActivity: {}", id);
        String sql = "delete from activities where id = ?";
        return jdbcTemplate.update(sql, id) == 1;
    }

    private ActivityVO activityTransform(ResultSet rs, int RowNum) throws SQLException {
        return new ActivityVO(
                rs.getInt("id"),
                rs.getString("name"),
                rs.getString("description")
        );
    }
}
